<?php
declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\common\Backend;
use app\common\model\Customer as ModelCustomer;

use app\common\model\PublicStatus;
use app\common\model\User as ModelUser;
use general\Random;
use Exception;
use think\facade\Db;
use think\db\exception\PDOException;
use think\exception\ValidateException;
class Customer extends Backend
{
        /**
     * @var Navigation
     */
    protected $model = null;
    protected $withJoinTable = ['user'];
    protected $quickSearchField = ['name'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new ModelCustomer();

    }

     /**
     * 数据源
     * @throws Throwable
     */
    public function index(): void
    {

        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit)->each(function ($item,$key){
                $item['username'] = $item['name'];
                return $item;
            });

        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => '',
        ]);
    }


     //详情
     public function detail($id = null): void
     {
         $row = $this->model->where('id', $id)->find();
         if (!$row) {
             $this->error(('Record not found'));
         }
         if($row->user){
            $row['user'] =$row->user;
         }
         $this->success('', [
             'row' => $row
         ]);
     }



     //处理申请并创建账号 -- 废弃
     public function handleCustomer() : void {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // $password = 'YM123456';
            // $salt   = Random::build('alnum', 16);
            // $passwd = encrypt_password($password, $salt);
            // $user_data['username'] = 'YM'.date('Ymd',time()). ($this->model->getMaxId() + 1); //用户编号
            // $user_data['label_id'] = Random::build('numeric', 9); //用户编号
            // $user_data['auth_code'] = Random::build('alnum', 12);  //授权码
            // $user_data['mobile'] = $data['mobile']; //手机号
            // $user_data['nickname'] = $data['company']; //手机号
            // $is_flag = $this->model->checkMobile($data['mobile']);
            // if ($is_flag) {
            //     $this->error('当前手机号已经存在，无法创建');
            // }
            $result = false;
            Db::startTrans();
            try {
                // $user_data['salt']     = $salt;
                // $user_data['password'] = $passwd;
                // $result = $this->model->save($user_data);
                $row = $this->model->where('id', $data['id'])->find();
                if (!$row) {
                    $this->error('未找到当前申请用户');
                }
                $row->status = PublicStatus::PUBLIC_STATUS_ONE;
                // $row->user_id = $this->model->id;
                //更新客户关联的id
                $result = $row->save();
                Db::commit();
            } catch (ValidateException | Exception | PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success('处理成功');
            } else {
                $this->error('处理失败');
            }
        }
        $this->error('未知');
     }
}
